package com.atguigu.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.AttrGroupRelationVo;
import com.atguigu.gulimall.product.vo.AttrGroupWitchattrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 属性分组
 *
 * @author mayp
 * @email 1582240996@qq.com
 * @date 2020-06-05 17:09:03
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService relationService;

    @PostMapping("/attr/relation")
    public R relation(@RequestBody List<AttrGroupRelationVo> vos ){
        relationService.saveBatch(vos);
        return R.ok();
    }

    /**
     * 获取分类下所有分组&关联属性
     *  /product/attrgroup/{catelogId}/withattr
     *  catelogId分类id
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWitchAttrs(@PathVariable("catelogId") Long catelogId){
//        1、查出当前分类下的所有属性分组
//        2、查询每个分组的所有属性
        List<AttrGroupWitchattrVo> data = attrGroupService.getAttrGroupWitchAttrsByCatelogId(catelogId);
//        封装List返回
        return R.ok().put("data",data);
    }

    /**
     * 列表
     *  根据查询条件查询。
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId){
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

//		得到当前的属性id
        Long catelogId = attrGroup.getCatelogId();
//        通过当前的属性id查找当前分类的所有id
        Long[] path = categoryService.findCatelogPath(catelogId);
//        设置到当前的属性对象
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 获取属性分组的关联的所有属性
     * 通过属性分组id获取属性与属性组的关联信息，即是这张表 pms_attr_attrgroup_relation
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R relation(@PathVariable("attrgroupId") Long attrgroupId){
//        通过属性组id查询属性组与属性的关联信息
        List<AttrEntity> entities = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data",entities);
    }

    /**
     * 获取属性分组没有关联的其他属性
     *  因为本属性组已经关联的不需要在新建关联中显示。
     *  从pms_attr_attrgroup_relation找出没有pms_attr的id
     *
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R noattrRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String,Object> param){
//        通过属性组id查询属性组与属性的关联信息,结果是分页的
        PageUtils page = attrService.getNoattrRelationAttr(attrgroupId,param);
        return R.ok().put("page",page);
    }

    /**
     * 删除属性与分组的关联关系
     * /product/attrgroup/attr/relation/delete
     */
    @PostMapping("/attr/relation/delete")
    public R deleteAttrGroupList(@RequestBody AttrGroupRelationVo[] attrGroupRelationVo){
        attrGroupService.deleteAttrGroupList(attrGroupRelationVo);
        return R.ok();
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
